﻿using System.Windows;
using System.Windows.Media;

namespace LogoFX.Controls
{
    public static class DependencyObjectExtensions
    {
        public static T FindAncestor<T>(this DependencyObject obj, bool includeThis = false)
        {
            bool firstIteration = true;

            while (null != obj)
            {
                if (obj is T && (includeThis || !firstIteration))
                {
                    return (T)(object)obj;
                }

                obj = VisualTreeHelper.GetParent(obj);
                firstIteration = false;
            }

            return default(T);
        }
    }
}
